1 using UnityEditor;
2 using
UnityEngine;
3
4 [CustomEditor(
typeof (PhotonTransformView))]
5 public
class PhotonTransformViewEditor : Editor
6 {
7     
private PhotonTransformView m_Target;
8
9     
private SerializedProperty m_SynchronizePositionProperty;
10     
private SerializedProperty m_SynchronizeRotationProperty;
11     
private SerializedProperty m_SynchronizeScaleProperty;
12
13     
private bool m_InterpolateHelpOpen;
14     
private bool m_ExtrapolateHelpOpen;
15     
private bool m_InterpolateRotationHelpOpen;
16     
private bool m_InterpolateScaleHelpOpen;
17
18     
private const int EDITOR_LINE_HEIGHT = 20;
19
20     
private const string INTERPOLATE_TOOLTIP =
21         
"Choose between synchronizing the value directly (by disabling interpolation) or smoothly move it towards the newest update.";
22
23     
private const string INTERPOLATE_HELP =
24         
"You can use interpolation to smoothly move your GameObject towards a new position that is received via the network. "
25         +
"This helps to reduce the stuttering movement that results because the network updates only arrive 10 times per second.\n"
26         +
"As a side effect, the GameObject is always lagging behind the actual position a little bit. This can be addressed with extrapolation.";
27
28     
private const string EXTRAPOLATE_TOOLTIP = "Extrapolation is used to predict where the GameObject actually is";
29
30     
private const string EXTRAPOLATE_HELP =
31         
"Whenever you deal with network values, all values you receive will be a little bit out of date since that data needs "
32         +
"to reach you first. You can use extrapolation to try to predict where the player actually is, based on the movement data you have received.\n"
33         +
34         
"This has to be tweaked carefully for each specific game in order to insure the optimal prediction. Sometimes it is very easy to extrapolate states, because "
35         +
36         
"the GameObject behaves very predictable (for example for vehicles). Other times it can be very hard because the user input is translated directly to the game "
37         +
"and you cannot really predict what the user is going to do (for example in fighting games)";
38
39     
private const string INTERPOLATE_HELP_URL = "http://doc.exitgames.com/en/pun/current/tutorials/rpg-movement";
40     
private const string EXTRAPOLATE_HELP_URL = "http://doc.exitgames.com/en/pun/current/tutorials/rpg-movement";
41
42     
public void OnEnable()
43     {
44         SetupSerializedProperties();
45     }
46
47     
public override void OnInspectorGUI()
48     {
49         
this.m_Target = (PhotonTransformView) target;
50
51         DrawIsPlayingWarning();
52         GUI.enabled = !Application.isPlaying;
53
54         DrawSynchronizePositionHeader();
55         DrawSynchronizePositionData();
56
57         GUI.enabled = !Application.isPlaying;
58         DrawSynchronizeRotationHeader();
59         DrawSynchronizeRotationData();
60
61         GUI.enabled = !Application.isPlaying;
62         DrawSynchronizeScaleHeader();
63         DrawSynchronizeScaleData();
64
65         serializedObject.ApplyModifiedProperties();
66
67         GUI.enabled =
true;
68     }
69
70     
private void DrawIsPlayingWarning()
71     {
72         
if (Application.isPlaying == false)
73         {
74             
return;
75         }
76
77         GUILayout.BeginVertical(GUI.skin.box);
78         {
79             GUILayout.Label(
"Editing is disabled in play mode so the two objects don't go out of sync");
80         }
81         GUILayout.EndVertical();
82     }
83
84     
private void SetupSerializedProperties()
85     {
86         
this.m_SynchronizePositionProperty = serializedObject.FindProperty("m_PositionModel.SynchronizeEnabled");
87         
this.m_SynchronizeRotationProperty = serializedObject.FindProperty("m_RotationModel.SynchronizeEnabled");
88         
this.m_SynchronizeScaleProperty = serializedObject.FindProperty("m_ScaleModel.SynchronizeEnabled");
89     }
90
91     
private void DrawSynchronizePositionHeader()
92     {
93         DrawHeader(
"Synchronize Position", this.m_SynchronizePositionProperty);
94     }
95
96     
private void DrawSynchronizePositionData()
97     {
98         
if (this.m_SynchronizePositionProperty == null || this.m_SynchronizePositionProperty.boolValue == false)
99         {
100             
return;
101         }
102
103         SerializedProperty interpolatePositionProperty = serializedObject.FindProperty(
"m_PositionModel.InterpolateOption");
104         PhotonTransformViewPositionModel.InterpolateOptions interpolateOption = (PhotonTransformViewPositionModel.InterpolateOptions)interpolatePositionProperty.enumValueIndex;
105
106         SerializedProperty extrapolatePositionProperty = serializedObject.FindProperty(
"m_PositionModel.ExtrapolateOption");
107         PhotonTransformViewPositionModel.ExtrapolateOptions extrapolateOption = (PhotonTransformViewPositionModel.ExtrapolateOptions)extrapolatePositionProperty.enumValueIndex;
108
109         
float containerHeight = 155;
110
111         
switch (interpolateOption)
112         {
113             
case PhotonTransformViewPositionModel.InterpolateOptions.FixedSpeed:
114             
case PhotonTransformViewPositionModel.InterpolateOptions.Lerp:
115                 containerHeight += EDITOR_LINE_HEIGHT;
116                 
break;
117             
/*case PhotonTransformViewPositionModel.InterpolateOptions.MoveTowardsComplex:
118                 containerHeight += EDITOR_LINE_HEIGHT*
3;
119                 
break;*/
120         }
121
122         
if (extrapolateOption != PhotonTransformViewPositionModel.ExtrapolateOptions.Disabled)
123         {
124             containerHeight += EDITOR_LINE_HEIGHT;
125         }
126
127         
switch (extrapolateOption)
128         {
129             
case PhotonTransformViewPositionModel.ExtrapolateOptions.FixedSpeed:
130                 containerHeight += EDITOR_LINE_HEIGHT;
131                 
break;
132         }
133
134         
if (this.m_InterpolateHelpOpen == true)
135         {
136             containerHeight += GetInterpolateHelpBoxHeight();
137         }
138
139         
if (this.m_ExtrapolateHelpOpen == true)
140         {
141             containerHeight += GetExtrapolateHelpBoxHeight();
142         }
143
144         
// removed Gizmo Options. -3 lines, -1 splitter
145         containerHeight -= EDITOR_LINE_HEIGHT *
2;
146
147         Rect rect = PhotonGUI.ContainerBody(containerHeight);
148
149         Rect propertyRect =
new Rect(rect.xMin + 5, rect.yMin + 2, rect.width - 10, EditorGUIUtility.singleLineHeight);
150
151         DrawTeleport(
ref propertyRect);
152         DrawSplitter(
ref propertyRect);
153
154         DrawSynchronizePositionDataInterpolation(
ref propertyRect, interpolatePositionProperty, interpolateOption);
155         DrawSplitter(
ref propertyRect);
156
157         DrawSynchronizePositionDataExtrapolation(
ref propertyRect, extrapolatePositionProperty, extrapolateOption);
158         DrawSplitter(
ref propertyRect);
159
160         DrawSynchronizePositionDataGizmos(
ref propertyRect);
161     }
162
163     
private float GetInterpolateHelpBoxHeight()
164     {
165         
return PhotonGUI.RichLabel.CalcHeight(new GUIContent(INTERPOLATE_HELP), Screen.width - 54) + 35;
166     }
167
168     
private float GetExtrapolateHelpBoxHeight()
169     {
170         
return PhotonGUI.RichLabel.CalcHeight(new GUIContent(EXTRAPOLATE_HELP), Screen.width - 54) + 35;
171     }
172
173     
private void DrawSplitter(ref Rect propertyRect)
174     {
175         Rect splitterRect =
new Rect(propertyRect.xMin - 3, propertyRect.yMin, propertyRect.width + 6, 1);
176         PhotonGUI.DrawSplitter(splitterRect);
177
178         propertyRect.y +=
5;
179     }
180
181     
private void DrawSynchronizePositionDataGizmos(ref Rect propertyRect)
182     {
183         GUI.enabled =
true;
184         
/*PhotonGUI.DrawGizmoOptions(propertyRect, "Synchronized Position Gizmo",
185             serializedObject.FindProperty(
"m_PositionModel.DrawNetworkGizmo"),
186             serializedObject.FindProperty(
"m_PositionModel.NetworkGizmoColor"),
187             serializedObject.FindProperty(
"m_PositionModel.NetworkGizmoType"),
188             serializedObject.FindProperty(
"m_PositionModel.NetworkGizmoSize"));
189         propertyRect.y += EDITOR_LINE_HEIGHT;
190
191         PhotonGUI.DrawGizmoOptions(propertyRect,
"Extrapolated Position Gizmo",
192             serializedObject.FindProperty(
"m_PositionModel.DrawExtrapolatedGizmo"),
193             serializedObject.FindProperty(
"m_PositionModel.ExtrapolatedGizmoColor"),
194             serializedObject.FindProperty(
"m_PositionModel.ExtrapolatedGizmoType"),
195             serializedObject.FindProperty(
"m_PositionModel.ExtrapolatedGizmoSize"));
196         propertyRect.y += EDITOR_LINE_HEIGHT;*/

197
198         EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_PositionModel.DrawErrorGizmo"),
199             
new GUIContent("Draw synchronized position error"));
200         propertyRect.y += EDITOR_LINE_HEIGHT;
201     }
202
203     
private void DrawHelpBox(ref Rect propertyRect, bool isOpen, float height, string helpText, string url)
204     {
205         
if (isOpen == true)
206         {
207             Rect helpRect =
new Rect(propertyRect.xMin, propertyRect.yMin, propertyRect.width, height - 5);
208             GUI.BeginGroup(helpRect, GUI.skin.box);
209             GUI.Label(
new Rect(5, 5, propertyRect.width - 10, height - 30), helpText, PhotonGUI.RichLabel);
210             
if (GUI.Button(new Rect(5, height - 30, propertyRect.width - 10, 20), "Read more in our documentation"))
211             {
212                 Application.OpenURL(url);
213             }
214             GUI.EndGroup();
215
216             propertyRect.y += height;
217         }
218     }
219
220     
private void DrawPropertyWithHelpIcon(ref Rect propertyRect, ref bool isHelpOpen, SerializedProperty property, string tooltip)
221     {
222         Rect propertyFieldRect =
new Rect(propertyRect.xMin, propertyRect.yMin, propertyRect.width - 20, propertyRect.height);
223         
string propertyName = ObjectNames.NicifyVariableName(property.name);
224         EditorGUI.PropertyField(propertyFieldRect, property,
new GUIContent(propertyName, tooltip));
225
226         Rect helpIconRect =
new Rect(propertyFieldRect.xMax + 5, propertyFieldRect.yMin, 20, propertyFieldRect.height);
227         isHelpOpen = GUI.Toggle(helpIconRect, isHelpOpen, PhotonGUI.HelpIcon, GUIStyle.none);
228
229         propertyRect.y += EDITOR_LINE_HEIGHT;
230     }
231
232     
private void DrawSynchronizePositionDataExtrapolation(ref Rect propertyRect, SerializedProperty extrapolatePositionProperty, PhotonTransformViewPositionModel.ExtrapolateOptions extrapolateOption)
233     {
234         DrawPropertyWithHelpIcon(
ref propertyRect, ref this.m_ExtrapolateHelpOpen, extrapolatePositionProperty, EXTRAPOLATE_TOOLTIP);
235         DrawHelpBox(
ref propertyRect, this.m_ExtrapolateHelpOpen, GetExtrapolateHelpBoxHeight(), EXTRAPOLATE_HELP, EXTRAPOLATE_HELP_URL);
236
237         
if (extrapolateOption != PhotonTransformViewPositionModel.ExtrapolateOptions.Disabled)
238         {
239             EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_PositionModel.ExtrapolateIncludingRoundTripTime"));
240             propertyRect.y += EDITOR_LINE_HEIGHT;
241         }
242
243         
switch (extrapolateOption)
244         {
245             
case PhotonTransformViewPositionModel.ExtrapolateOptions.FixedSpeed:
246                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_PositionModel.ExtrapolateSpeed"));
247                 propertyRect.y += EDITOR_LINE_HEIGHT;
248                 
break;
249         }
250     }
251
252     
private void DrawTeleport(ref Rect propertyRect)
253     {
254         EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_PositionModel.TeleportEnabled"),
255             
new GUIContent("Enable teleport for great distances"));
256         propertyRect.y += EDITOR_LINE_HEIGHT;
257
258         EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_PositionModel.TeleportIfDistanceGreaterThan"),
259             
new GUIContent("Teleport if distance greater than"));
260         propertyRect.y += EDITOR_LINE_HEIGHT;
261     }
262
263     
private void DrawSynchronizePositionDataInterpolation(ref Rect propertyRect, SerializedProperty interpolatePositionProperty,
264         PhotonTransformViewPositionModel.InterpolateOptions interpolateOption)
265     {
266         DrawPropertyWithHelpIcon(
ref propertyRect, ref this.m_InterpolateHelpOpen, interpolatePositionProperty, INTERPOLATE_TOOLTIP);
267         DrawHelpBox(
ref propertyRect, this.m_InterpolateHelpOpen, GetInterpolateHelpBoxHeight(), INTERPOLATE_HELP, INTERPOLATE_HELP_URL);
268
269         
switch (interpolateOption)
270         {
271             
case PhotonTransformViewPositionModel.InterpolateOptions.FixedSpeed:
272                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_PositionModel.InterpolateMoveTowardsSpeed"),
273                     
new GUIContent("MoveTowards Speed"));
274                 propertyRect.y += EDITOR_LINE_HEIGHT;
275                 
break;
276
277             
case PhotonTransformViewPositionModel.InterpolateOptions.Lerp:
278                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_PositionModel.InterpolateLerpSpeed"), new GUIContent("Lerp Speed"));
279                 propertyRect.y += EDITOR_LINE_HEIGHT;
280                 
break;
281
282             
/*case PhotonTransformViewPositionModel.InterpolateOptions.MoveTowardsComplex:
283                 Rect curveRect =
new Rect(propertyRect.xMin, propertyRect.yMin, propertyRect.width - 100, propertyRect.height);
284                 EditorGUI.PropertyField(curveRect, serializedObject.FindProperty(
"m_PositionModel.InterpolateSpeedCurve"), new GUIContent("MoveTowards Speed Curve"));
285
286                 Rect labelRect =
new Rect(propertyRect.xMax - 95, propertyRect.yMin, 10, propertyRect.height);
287                 GUI.Label(labelRect,
"x");
288
289                 Rect multiplierRect =
new Rect(propertyRect.xMax - 80, propertyRect.yMin, 80, propertyRect.height);
290                 EditorGUI.PropertyField(multiplierRect, serializedObject.FindProperty(
"m_PositionModel.InterpolateMoveTowardsSpeed"), GUIContent.none);
291                 propertyRect.y += EDITOR_LINE_HEIGHT;
292
293                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_PositionModel.InterpolateMoveTowardsAcceleration"),
294                     
new GUIContent("Acceleration"));
295                 propertyRect.y += EDITOR_LINE_HEIGHT;
296
297                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_PositionModel.InterpolateMoveTowardsDeceleration"),
298                     
new GUIContent("Deceleration"));
299                 propertyRect.y += EDITOR_LINE_HEIGHT;
300                 
break;*/
301         }
302     }
303
304     
private void DrawSynchronizeRotationHeader()
305     {
306         DrawHeader(
"Synchronize Rotation", this.m_SynchronizeRotationProperty);
307     }
308
309     
private void DrawSynchronizeRotationData()
310     {
311         
if (this.m_SynchronizeRotationProperty == null || this.m_SynchronizeRotationProperty.boolValue == false)
312         {
313             
return;
314         }
315
316         SerializedProperty interpolateRotationProperty = serializedObject.FindProperty(
"m_RotationModel.InterpolateOption");
317         PhotonTransformViewRotationModel.InterpolateOptions interpolateOption =
318             (PhotonTransformViewRotationModel.InterpolateOptions) interpolateRotationProperty.enumValueIndex;
319
320         
float containerHeight = 20;
321
322         
switch (interpolateOption)
323         {
324             
case PhotonTransformViewRotationModel.InterpolateOptions.RotateTowards:
325             
case PhotonTransformViewRotationModel.InterpolateOptions.Lerp:
326                 containerHeight += EDITOR_LINE_HEIGHT;
327                 
break;
328         }
329
330         
if (this.m_InterpolateRotationHelpOpen == true)
331         {
332             containerHeight += GetInterpolateHelpBoxHeight();
333         }
334
335         Rect rect = PhotonGUI.ContainerBody(containerHeight);
336         Rect propertyRect =
new Rect(rect.xMin + 5, rect.yMin + 2, rect.width - 10, EditorGUIUtility.singleLineHeight);
337
338         DrawPropertyWithHelpIcon(
ref propertyRect, ref this.m_InterpolateRotationHelpOpen, interpolateRotationProperty, INTERPOLATE_TOOLTIP);
339         DrawHelpBox(
ref propertyRect, this.m_InterpolateRotationHelpOpen, GetInterpolateHelpBoxHeight(), INTERPOLATE_HELP, INTERPOLATE_HELP_URL);
340
341         
switch (interpolateOption)
342         {
343             
case PhotonTransformViewRotationModel.InterpolateOptions.RotateTowards:
344                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_RotationModel.InterpolateRotateTowardsSpeed"),
345                     
new GUIContent("RotateTowards Speed"));
346                 
break;
347             
case PhotonTransformViewRotationModel.InterpolateOptions.Lerp:
348                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_RotationModel.InterpolateLerpSpeed"), new GUIContent("Lerp Speed"));
349                 
break;
350         }
351     }
352
353     
private void DrawSynchronizeScaleHeader()
354     {
355         DrawHeader(
"Synchronize Scale", this.m_SynchronizeScaleProperty);
356     }
357
358     
private void DrawSynchronizeScaleData()
359     {
360         
if (this.m_SynchronizeScaleProperty == null || this.m_SynchronizeScaleProperty.boolValue == false)
361         {
362             
return;
363         }
364
365         SerializedProperty interpolateScaleProperty = serializedObject.FindProperty(
"m_ScaleModel.InterpolateOption");
366         PhotonTransformViewScaleModel.InterpolateOptions interpolateOption = (PhotonTransformViewScaleModel.InterpolateOptions) interpolateScaleProperty.enumValueIndex;
367
368         
float containerHeight = EDITOR_LINE_HEIGHT;
369
370         
switch (interpolateOption)
371         {
372             
case PhotonTransformViewScaleModel.InterpolateOptions.MoveTowards:
373             
case PhotonTransformViewScaleModel.InterpolateOptions.Lerp:
374                 containerHeight += EDITOR_LINE_HEIGHT;
375                 
break;
376         }
377
378         
if (this.m_InterpolateScaleHelpOpen == true)
379         {
380             containerHeight += GetInterpolateHelpBoxHeight();
381         }
382
383         Rect rect = PhotonGUI.ContainerBody(containerHeight);
384         Rect propertyRect =
new Rect(rect.xMin + 5, rect.yMin + 2, rect.width - 10, EditorGUIUtility.singleLineHeight);
385
386         DrawPropertyWithHelpIcon(
ref propertyRect, ref this.m_InterpolateScaleHelpOpen, interpolateScaleProperty, INTERPOLATE_TOOLTIP);
387         DrawHelpBox(
ref propertyRect, this.m_InterpolateScaleHelpOpen, GetInterpolateHelpBoxHeight(), INTERPOLATE_HELP, INTERPOLATE_HELP_URL);
388
389         
switch (interpolateOption)
390         {
391             
case PhotonTransformViewScaleModel.InterpolateOptions.MoveTowards:
392                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_ScaleModel.InterpolateMoveTowardsSpeed"),
393                     
new GUIContent("MoveTowards Speed"));
394                 
break;
395             
case PhotonTransformViewScaleModel.InterpolateOptions.Lerp:
396                 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty(
"m_ScaleModel.InterpolateLerpSpeed"), new GUIContent("Lerp Speed"));
397                 
break;
398         }
399     }
400
401     
private void DrawHeader(string label, SerializedProperty property)
402     {
403         
if (property == null)
404         {
405             
return;
406         }
407
408         
bool newValue = PhotonGUI.ContainerHeaderToggle(label, property.boolValue);
409
410         
if (newValue != property.boolValue)
411         {
412             Undo.RecordObject(
this.m_Target, "Change " + label);
413             property.boolValue = newValue;
414             EditorUtility.SetDirty(
this.m_Target);
415         }
416     }
417 }


private const string INTERPOLATE_HELP_URL = "http:doc.exitgames.comenpuncurrenttutorialsrpg-movement";

private const string EXTRAPOLATE_HELP_URL = "http:doc.exitgames.comenpuncurrenttutorialsrpg-movement";

removed Gizmo Options. -3 lines, -1 splitter




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.541 lượt xem

Gõ tìm kiếm nhanh...